package view.tthc;

import java.util.List;

import javax.faces.event.ActionEvent;

import oracle.adf.model.binding.DCIteratorBinding;
import oracle.adf.view.rich.component.rich.RichPopup;
import oracle.adf.view.rich.event.DialogEvent;
import oracle.adf.view.rich.event.PopupCanceledEvent;

import oracle.jbo.ViewCriteria;
import oracle.jbo.ViewCriteriaItem;
import oracle.jbo.ViewCriteriaRow;
import oracle.jbo.ViewObject;

import oracle.jbo.common.JboCompOper;

import view.common.Utils;

public class coquanbean {
    private RichPopup newPopupVN;
    private RichPopup newPopupEN;
    private RichPopup newPopupDeleteVN;
    private RichPopup newPopupDeleteEN;
    private static int operationType = 0; //0: CreateInsert, 1: Update
    private String tieudeVN;
    private String tieudeEN;
    private Object tukhoaVN;
    private Object tukhoaEN;

    public coquanbean() {
        if (Boolean.FALSE.equals(Utils.getElExpression("#{adfFacesContext.postback}"))) {
            DCIteratorBinding coquanIteratorVN =
                Utils.getIteratorBinding("TthcCoquanView_VN1Iterator");
            coquanIteratorVN.getViewObject().getViewCriteriaManager().clearViewCriterias();
            coquanIteratorVN.executeQuery();

            DCIteratorBinding coquanIteratorEN =
                Utils.getIteratorBinding("TthcCoquanView_EN1Iterator");
            coquanIteratorEN.getViewObject().getViewCriteriaManager().clearViewCriterias();
            coquanIteratorEN.executeQuery();
        }
    }

    public void createVN(ActionEvent actionEvent) {
        Utils.execute("CreateInsertVN");
        Utils.showPopup(newPopupVN);
        operationType = 0;
    }

    public void createEN(ActionEvent actionEvent) {
        Utils.execute("CreateInsertEN");
        Utils.showPopup(newPopupEN);
        operationType = 0;
    }

    public void updateVN(ActionEvent actionEvent) {
        if (Utils.getIteratorBinding("TthcCoquanView_VN1Iterator").getCurrentRow() ==
            null) {
            String messageText = "Bạn chưa chọn cơ quan nào.";
            Utils.showMessage(Utils.MSG_ERROR, messageText);
        } else {
            operationType = 1;
            Utils.showPopup(newPopupVN);
        }
    }

    public void updateEN(ActionEvent actionEvent) {
        if (Utils.getIteratorBinding("TthcCoquanView_EN1Iterator").getCurrentRow() ==
            null) {
            String messageText = "Bạn chưa chọn cơ quan nào.";
            Utils.showMessage(Utils.MSG_ERROR, messageText);
        } else {
            operationType = 1;
            Utils.showPopup(newPopupEN);
        }
    }

    public void commitVN(DialogEvent dialogEvent) {
        DCIteratorBinding coquanIterator =
            Utils.getIteratorBinding("TthcCoquanView_VN1Iterator");
        boolean validated = true;
        if (coquanIterator.getAllRowsInRange().length > 0) {
            if (tieudeVN.equals(coquanIterator.getCurrentRow().getAttribute("Tieude")) ==
                false) {
                if (Utils.checkExisted(tieudeVN, "Tieude",
                                       "TthcCoquanValidate1Iterator", "vi")) {
                    validated = false;
                    Utils.showMessage(Utils.MSG_ERROR,
                                      "Tiêu đề bạn vừa nhập đã tồn tại trong hệ thống.");
                }
            }
        }
        if (validated) {
            coquanIterator.getCurrentRow().setAttribute("Tieude", tieudeVN);
            if (operationType == 0) {
                coquanIterator.getCurrentRow().setAttribute("NguoiNhap",
                                                            Utils.getCurrentUser());
                coquanIterator.getCurrentRow().setAttribute("NgayNhap",
                                                            Utils.getCurrentDate());
                coquanIterator.getCurrentRow().setAttribute("NgonNgu", "vi");
            }
            Utils.execute("Commit");
            Utils.iteratorRefresh("TthcCoquanView_VN1Iterator");
            Utils.iteratorRefresh("TthcCoquanView_EN1Iterator");
            operationType = 0;
        }
    }

    public void commitEN(DialogEvent dialogEvent) {
        DCIteratorBinding coquanIterator =
            Utils.getIteratorBinding("TthcCoquanView_EN1Iterator");
        boolean validated = true;
        if (coquanIterator.getAllRowsInRange().length > 0) {
            if (tieudeEN.equals(coquanIterator.getCurrentRow().getAttribute("Tieude")) ==
                false) {
                if (Utils.checkExisted(tieudeEN, "Tieude",
                                       "TthcCoquanValidate1Iterator", "en")) {
                    validated = false;
                    Utils.showMessage(Utils.MSG_ERROR,
                                      "Tiêu đề bạn vừa nhập đã tồn tại trong hệ thống.");
                }
            }
        }
        if (validated) {
            coquanIterator.getCurrentRow().setAttribute("Tieude", tieudeEN);
            if (operationType == 0) {
                coquanIterator.getCurrentRow().setAttribute("NguoiNhap",
                                                            Utils.getCurrentUser());
                coquanIterator.getCurrentRow().setAttribute("NgayNhap",
                                                            Utils.getCurrentDate());
                coquanIterator.getCurrentRow().setAttribute("NgonNgu", "en");
            }
            Utils.execute("Commit");
            Utils.iteratorRefresh("TthcCoquanView_EN1Iterator");
            Utils.iteratorRefresh("TthcCoquanView_VN1Iterator");
            operationType = 0;
        }
    }

    public void cancelDiaglog(PopupCanceledEvent popupCanceledEvent) {
        Utils.execute("Rollback");
        operationType = 0;
    }

    public void deleteVN(DialogEvent dialogEvent) {
        DCIteratorBinding coquanIterator =
            Utils.getIteratorBinding("TthcCoquanView_VN1Iterator");
        if (coquanIterator.getCurrentRow() != null) {
            String coquanId =
                coquanIterator.getCurrentRow().getAttribute("Id").toString();
            DCIteratorBinding thutucIterator =
                Utils.getIteratorBinding("TthcThutucView_VN1Iterator");
            ViewObject vo = thutucIterator.getViewObject();
            ViewCriteria vc = vo.createViewCriteria();
            vc.setName("ThutucViewCriteriaVN");
            ViewCriteriaRow vcRow = vc.createViewCriteriaRow();
            ViewCriteriaItem vcItem = vcRow.ensureCriteriaItem("CoquanId");
            vcItem.setOperator(JboCompOper.OPER_CONTAINS_DELIMITED_ID);
            vcItem.setValue(coquanId);
            vc.addRow(vcRow);
            vo.getViewCriteriaManager().removeApplyViewCriteriaName("ThutucViewCriteriaVN");
            vo.applyViewCriteria(vc);
            vo.executeQuery();
            if (vo.getAllRowsInRange().length > 0) {
                Utils.showMessage(Utils.MSG_ERROR,
                                  "Có thủ tục đang thuộc cơ quan này. Bạn không thể xóa cơ quan này.");
            } else {
                Utils.execute("DeleteVN");
                Utils.execute("Commit");
            }
        }
        Utils.iteratorRefresh("TthcCoquanView_VN1Iterator");
        operationType = 0;
    }

    public void deleteEN(DialogEvent dialogEvent) {
        DCIteratorBinding coquanIterator =
            Utils.getIteratorBinding("TthcCoquanView_EN1Iterator");
        if (coquanIterator.getCurrentRow() != null) {
            String coquanId =
                coquanIterator.getCurrentRow().getAttribute("Id").toString();
            DCIteratorBinding thutucIterator =
                Utils.getIteratorBinding("TthcThutucView_EN1Iterator");
            ViewObject vo = thutucIterator.getViewObject();
            ViewCriteria vc = vo.createViewCriteria();
            vc.setName("ThutucViewCriteriaEN");
            ViewCriteriaRow vcRow = vc.createViewCriteriaRow();
            ViewCriteriaItem vcItem = vcRow.ensureCriteriaItem("CoquanId");
            vcItem.setOperator(JboCompOper.OPER_CONTAINS_DELIMITED_ID);
            vcItem.setValue(coquanId);
            vc.addRow(vcRow);
            vo.getViewCriteriaManager().removeApplyViewCriteriaName("ThutucViewCriteriaEN");
            vo.applyViewCriteria(vc);
            vo.executeQuery();
            if (vo.getAllRowsInRange().length > 0) {
                Utils.showMessage(Utils.MSG_ERROR,
                                  "Có thủ tục đang thuộc cơ quan này. Bạn không thể xóa cơ quan này.");
            } else {
                Utils.execute("DeleteEN");
                Utils.execute("Commit");
            }
        }
        Utils.iteratorRefresh("TthcCoquanView_EN1Iterator");
        operationType = 0;
    }

    public void setNewPopupVN(RichPopup newPopupVN) {
        this.newPopupVN = newPopupVN;
    }

    public RichPopup getNewPopupVN() {
        return newPopupVN;
    }

    public void setNewPopupEN(RichPopup newPopupEN) {
        this.newPopupEN = newPopupEN;
    }

    public RichPopup getNewPopupEN() {
        return newPopupEN;
    }

    public static void setOperationType(int operationType) {
        coquanbean.operationType = operationType;
    }

    public static int getOperationType() {
        return operationType;
    }

    public void setTieudeVN(String tieudeVN) {
        this.tieudeVN = tieudeVN;
    }

    public String getTieudeVN() {
        if (operationType == 1) {
            DCIteratorBinding coquanIterator =
                Utils.getIteratorBinding("TthcCoquanView_VN1Iterator");
            if (coquanIterator.getAllRowsInRange().length > 0) {
                tieudeVN =
                        coquanIterator.getCurrentRow().getAttribute("Tieude").toString();
            }
        }
        return tieudeVN;
    }

    public void setTieudeEN(String tieudeEN) {
        this.tieudeEN = tieudeEN;
    }

    public String getTieudeEN() {
        if (operationType == 1) {
            DCIteratorBinding coquanIterator =
                Utils.getIteratorBinding("TthcCoquanView_EN1Iterator");
            if (coquanIterator.getAllRowsInRange().length > 0) {
                tieudeEN =
                        coquanIterator.getCurrentRow().getAttribute("Tieude").toString();
            }
        }
        return tieudeEN;
    }

    public void setNewPopupDeleteVN(RichPopup newPopupDeleteVN) {
        this.newPopupDeleteVN = newPopupDeleteVN;
    }

    public RichPopup getNewPopupDeleteVN() {
        return newPopupDeleteVN;
    }

    public void setNewPopupDeleteEN(RichPopup newPopupDeleteEN) {
        this.newPopupDeleteEN = newPopupDeleteEN;
    }

    public RichPopup getNewPopupDeleteEN() {
        return newPopupDeleteEN;
    }

    public void openDeletePopupVN(ActionEvent actionEvent) {
        if (Utils.getIteratorBinding("TthcCoquanView_VN1Iterator").getCurrentRow() ==
            null) {
            String messageText = "Bạn chưa chọn cơ quan nào.";
            Utils.showMessage(Utils.MSG_ERROR, messageText);
        } else {
            Utils.showPopup(newPopupDeleteVN);
        }
    }

    public void openDeletePopupEN(ActionEvent actionEvent) {
        if (Utils.getIteratorBinding("TthcCoquanView_EN1Iterator").getCurrentRow() ==
            null) {
            String messageText = "Bạn chưa chọn cơ quan nào.";
            Utils.showMessage(Utils.MSG_ERROR, messageText);
        } else {
            Utils.showPopup(newPopupDeleteEN);
        }
    }

    public void setTukhoaVN(Object tukhoaVN) {
        this.tukhoaVN = tukhoaVN;
    }

    public Object getTukhoaVN() {
        return tukhoaVN;
    }

    public void setTukhoaEN(Object tukhoaEN) {
        this.tukhoaEN = tukhoaEN;
    }

    public Object getTukhoaEN() {
        return tukhoaEN;
    }

    public void searchVN() {
        if (tukhoaVN == null) {
            tukhoaVN = "";
        }
        DCIteratorBinding baivietIterator =
            Utils.getIteratorBinding("TthcCoquanView_VN1Iterator");
        ViewObject vo = baivietIterator.getViewObject();
        ViewCriteria vc =
            vo.getViewCriteriaManager().getViewCriteria("TthcCoquanView_VNCriteria");
        vo.ensureVariableManager().setVariableValue("varTukhoa",
                                                    this.tukhoaVN);
        vo.getViewCriteriaManager().removeApplyViewCriteriaName("TthcCoquanView_VNCriteria");
        vo.getViewCriteriaManager().applyViewCriteria(vc);
        vo.setRangeSize(-1);
        vo.executeQuery();
    }

    public void searchEN() {
        if (tukhoaEN == null) {
            tukhoaEN = "";
        }
        DCIteratorBinding baivietIterator =
            Utils.getIteratorBinding("TthcCoquanView_EN1Iterator");
        ViewObject vo = baivietIterator.getViewObject();
        ViewCriteria vc =
            vo.getViewCriteriaManager().getViewCriteria("TthcCoquanView_ENCriteria");
        vo.ensureVariableManager().setVariableValue("varTukhoa",
                                                    this.tukhoaEN);
        vo.getViewCriteriaManager().removeApplyViewCriteriaName("TthcCoquanView_ENCriteria");
        vo.getViewCriteriaManager().applyViewCriteria(vc);
        vo.setRangeSize(-1);
        vo.executeQuery();
    }
}
